/**
 * 股票计算工具模块
 */

/**
 * 计算移动平均线
 * @param {number} day 天数
 * @param {Array} data 数据数组
 * @returns {Array} 移动平均线数据
 */
export const calculateMA = (day, data) => {
  const result = [];
  for (let i = 0, len = data.length; i < len; i++) {
    if (i < day - 1) {
      result.push('-');
      continue;
    }
    let sum = 0;
    for (let j = 0; j < day; j++) {
      sum += data[i - j].close;
    }
    result.push(+(sum / day).toFixed(2));
  }
  return result;
};

/**
 * 计算平均值
 * @param {Array} data 数据数组
 * @param {number} start 开始索引
 * @param {number} end 结束索引
 * @returns {string} 平均值
 */
export const calculateAverage = (data, start, end) => {
  const slice = data.slice(start + 1, end + 1);
  const sum = slice.reduce((acc, val) => acc + parseFloat(val), 0);
  return (sum / slice.length).toFixed(2);
};

/**
 * 生成重复数组
 * @param {*} value 重复的值
 * @param {number} count 重复次数
 * @returns {Array} 重复数组
 */
export const generateRepeatArray = (value, count = 20) => {
  return Array(count).fill(value);
};

/**
 * 计算历史数据
 * @param {Array} maxData 最大值数据
 * @param {Array} minData 最小值数据
 * @param {Array} currentData 当前值数据
 * @param {number} startTime 开始时间
 * @param {number} endTime 结束时间
 * @param {number} length 数据长度
 * @returns {Object} 历史数据计算结果
 */
export const calculateHistoricalData = (maxData, minData, currentData, startTime, endTime, length) => {
  const result = {
    max_array: [],
    min_array: [],
    this_array: [],
    zcw3: [],
    zsw_array: [],
    zcw_array: [],
    max_num1: [],
    min_num1: []
  };

  for (let i = 0; i < length - 20; i++) {
    const currentEnd = endTime - i;
    const currentStart = startTime - i;

    const maxValue = calculateAverage(maxData, currentStart, currentEnd);
    const minValue = calculateAverage(minData, currentStart, currentEnd);
    const currentValue = calculateAverage(currentData, currentStart, currentEnd);

    result.max_array.push(maxValue);
    result.min_array.push(minValue);
    result.this_array.push(currentValue);
    
    result.zsw_array.push(parseFloat(maxValue * 0.95).toFixed(2));
    result.zcw_array.push(parseFloat((maxValue - minValue) * 0.618 + parseFloat(minValue)).toFixed(2));

    const maxSlice = maxData.slice(currentEnd - 20, currentEnd);
    const minSlice = minData.slice(currentEnd - 20, currentEnd);

    const maxItem = Math.max(...maxSlice);
    const minItem = Math.min(...minSlice);

    result.max_num1.push(maxItem === -Infinity ? parseFloat(result.max_num1[result.max_num1.length - 1]).toFixed(2) : maxItem);
    result.min_num1.push(minItem === Infinity ? parseFloat(result.min_num1[result.min_num1.length - 1]) : minItem);
    
    result.zcw3.push((maxItem - minItem) * 0.618 + minItem);
  }

  // 补充数组到指定长度
  const lastZcw3 = result.zcw3[result.zcw3.length - 1];
  const lastZsw = result.zsw_array[result.zsw_array.length - 1];
  const lastZcw = result.zcw_array[result.zcw_array.length - 1];
  const lastMax = result.max_num1[result.max_num1.length - 1];
  const lastMin = result.min_num1[result.min_num1.length - 1];

  result.zcw3 = [...result.zcw3, ...generateRepeatArray(lastZcw3)];
  result.zsw_array = [...result.zsw_array, ...generateRepeatArray(lastZsw)];
  result.zcw_array = [...result.zcw_array, ...generateRepeatArray(lastZcw)];
  result.max_num1 = [...result.max_num1, ...generateRepeatArray(lastMax)];
  result.min_num1 = [...result.min_num1, ...generateRepeatArray(lastMin)];

  return result;
}; 